<template>
  <div class="comment">
    <div class="bottom">
      <input
        placeholder="想说的话,可以留在这里"
        type="text"
        v-model="value"
      /><span @click="addComment">发送</span>
    </div>
    <div class="hared">
     <div> <span
        class="iconfont icon-arrow-left-bold"
        @click="$router.go(-1)"
      ></span></div>
      <div><span>评论({{ commentData.total }})</span></div>
      <div class="right"><van-icon name="share-o" size="20px"/></div>
    </div>
    <div class="song"  >
      <div class="title" v-if="song.al.picUrl">
        <div class="img">
          <img :src="song.al.picUrl" alt="" width="100%" height="100%"/>
        </div>
        <div class="name">{{ song.name }}({{ song.ar[0].name }})</div>
      </div>
    </div>
    <div class="txt-box">
      <div class="">
        <div class="nav">评论区</div>
        <van-tabs v-model="activeName" background="transparent" line-height="0px">
          <van-tab title="最热" name="a" title-class="tables">
           
            <ul :style="getHot">
              <li v-for="(item, index) in commentData.hotComments" :key="index">
                <div class="img">
                  <img
                    :src="item.user.avatarUrl"
                    alt=""
                    width="100%"
                    height="100%"
                  />
                </div>
                <div class="text">
                  <div class="title">
                    <div class="name-time">
                      <div class="name">{{ item.user.nickname }}</div>
                      <div class="time">{{ item.time | time }}</div>
                    </div>
                    <div class="zan">
                      {{ item.likedCount }}
                      <!-- <van-icon
                  :name="[flgData[item.index] ? 'good-job' :'good-job-o']"
                  @click="add(item.index)"
                /> -->
                      <span
                        :class="[
                          'iconfont',
                          item.liked ? 'icon-dianzan_kuai' : 'icon-dianzan',
                        ]"
                        @click="add(index)"
                      ></span>
                    </div>
                  </div>
                  <div class="comm">
                    <div class="t1">
                      {{ item.content }}
                    </div>
                    <div class="t2">999条回复</div>
                  </div>
                </div>
              </li>
            </ul></van-tab
          >
          <van-tab title="最新" name="b" title-class="tables">
            <ul>
              <li v-for="(item, index) in commentData.comments" :key="index">
                <div class="img">
                  <img
                    :src="item.user.avatarUrl"
                    alt=""
                    width="100%"
                    height="100%"
                  />
                </div>
                <div class="text">
                  <div class="title">
                    <div class="name-time">
                      <div class="name">{{ item.user.nickname }}</div>
                      <div class="time">{{ item.timeStr }}</div>
                    </div>
                    <div class="zan">
                      {{ item.likedCount }}
                      <!-- <van-icon
                  :name="[flgData[item.index] ? 'good-job' :'good-job-o']"
                  @click="add(item.index)"
                /> -->
                      <span
                        :class="[
                          'iconfont',
                          item.liked ? 'icon-dianzan_kuai' : 'icon-dianzan',
                        ]"
                        @click="add(item.commentId,item.liked)"
                      ></span>
                    </div>
                  </div>
                  <div class="comm">
                    <div class="t1">
                      {{ item.content }}
                    </div>
                    <div class="t2">999条回复</div>
                  </div>
                </div>
              </li>
            </ul>
          </van-tab>
        </van-tabs>
      </div>
    </div>
  </div>
</template>

<script>
import { getComment, getSong, setComment, dainZan } from "../api/comment";
export default {
  data() {
    return {
      song: { ar:{picUrl: ""} },
      commentData: { total: "" },
      good: true,
      value: "",
      flgData: [],
      commentFlg: true,
      activeName:'b'
    };
  },
  methods: {
    getHot(){
      if(this.commentData.hotComments){
        return ''
      }else{
        return 'height:350px'
      }
    },
    add(cid,t) {
     let id = this.$route.query.id;
      let flg = Number(!t);
      console.log(cid,flg)
      // console.log(flg)
        dainZan({
          id,
          cid,
          t: flg,
          type: 0,
        }).then((data) => {
          console.log(data);
        });
    },
    edit() {
      let id = this.$route.query.id;
      this.flgData.forEach((item) => {
        let flg = Number(item.liked);
        dainZan({
          id,
          cid: item.commentId,
          t: flg,
          type: 0,
        }).then((data) => {
          console.log(data);
        });
      });
    },
    //评论歌曲
    addComment() {
      let id = this.$route.query.id;
      setComment({
        t: 1,
        type: 0,
        id,
        content: this.value,
      }).then((data) => {
        console.log(data);
      });
      this.value = "";
    },
  },
  // 过滤器
  filters: {
    time(value) {
      var date = new Date(value); //时间戳为10位需*1000，时间戳为13位的话不需乘1000

      let Y = date.getFullYear() + "年";

      let M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "月";

      let D = date.getDate() + "日";

      return Y + M + D; //时分秒可以根据自己的需求加上
    },
    playCountData(value) {
      let w = value > 10000 ? value / 10000 : value; // 万计算
      let y = w > 10000 ? parseInt(value / 10000) + "亿" : parseInt(w) + "万"; //亿计算
      return y;
    },
  },
  watch: {
    commentData() {
      this.commentData.hotComments.forEach((item) => {
        this.flgData.push({ cid: item.commentId, flg: item.liked });
      });
    },
  },
  created() {
    let id = this.$route.query.id;
    getSong({ ids: id }).then((data) => {
      console.log(data);
      this.song = data.songs[0];
    });
    getComment({ id }).then((data) => {
      console.log(data);
      this.commentData = data;
      console.log(this.commentData);
    });
    console.log(this.flgData);
  },
};
</script>
<style lang="less">
.comment {
  //   padding: 10px;
  position: relative;
  background-image: linear-gradient(#ebedf0, #fff);
  z-index: 4;
  padding-bottom: 50px;
  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 50px;
    width: 100%;
    z-index: 1;
    //   background-color: rgb(136, 17, 37);
    input {
      height: 100%;
      width: 100%;
     background-color: #f6f6f6;
      border: none;
    }
    span {
      position: fixed;
      //   bottom: 5px;
      right: 5px;
      font-size: 16px;
      height: 50px;
      line-height: 50px;
      color: rgb(83, 80, 80);
    }
  }
  .hared {
    display: flex;
    justify-content: space-between;
    font-size: 20px;
    font-weight: 500;
    padding-top: 16px;
    padding-left: 16px;
    .iconfont {
      font-size: 20px;
    }
    .right{
      padding-right: 16px;
    }
  }
  .song {
    padding: 10px 0;
    .title {
      display: flex;
      align-items: center;
      padding: 20px 10px;
      border-bottom: 4px solid #dad7d7;
      .img {
        width: 50px;
        height: 50px;
        margin-right: 5px;
        border-radius: 50%;
        background-color: red;
        overflow: hidden;
      }
    }
  }
  .txt-box {
    margin-top: 4px;
    padding: 10px;
    position: relative;
    .nav{
      position: absolute;
      left: 10px;
      font-size: 18px;
      font-weight: 500;
    }
    .van-tabs__nav--line {
    box-sizing: content-box;
    height: 100%;
    width: 100px;
    margin-left: 266px;
     padding-bottom: 15px;
       .van-tabs__line{
         color: transparent;
       }  
       }
    .tables{
      align-items: flex-start;
    }
    ul {
      li {
        display: flex;
        margin: 5px 0;
        .text {
          width: 100%;
        }
        .title {
          display: flex;
          justify-content: space-between;
          .name {
            color: rgb(123, 123, 123);
            font-size: 16px;
          }
          .time {
            color: rgb(123, 123, 123);
            font-size: 12px;
          }
          .zan {
            font-size: 14px;
            color: rgb(123, 123, 123);
          }
        }
        .img {
          width: 50px;
          height: 50px;
          margin-right: 5px;
          border-radius: 50%;
          background-color: red;
          flex-shrink: 0;
          overflow: hidden;
        }
        .comm {
          margin-top: 10px;
          border-bottom: 1px solid #ccc;
          .t1 {
            font-size: 15px;
            color: #4d4c4c;
          }
          .t2 {
            font-size: 14px;
            margin: 5px;
            color: rgb(110, 147, 189);
          }
        }
      }
    }
  }
}
</style>